from qiniu import Auth, put_data


class QiNiuAvatar:
    access_key = 'VFpq8FmPI2rYrhvC8OcbxKMOv-8eQBoWmIWsk8K-'
    secret_key = 'VymZzpDwkofXYdhnsiwTYbheGiuFYLRHZiNVIc6L'
    bucket_name = 'xunshi'

    def __new__(cls, *args, **kwargs):
        # 判断是否存在类属性_instance _instance是类CCP的唯一对象，即单例
        if not hasattr(QiNiuAvatar, "_instance"):
            cls._instance = super(QiNiuAvatar, cls).__new__(cls, *args, **kwargs)
            cls._instance.q = Auth(cls.access_key, cls.secret_key)
        return cls._instance

    def upload(self, img):
        token = self.q.upload_token(self.bucket_name, key=None)
        ret, info = put_data(token, None, img)
        return ret, info
